<?php

class Reservations_EditAction extends SrsReservationsBaseAction
{
	public function getCredentials()
	{
		return 'reservations.edit';
	}

	public function executeRead(AgaviRequestDataHolder $rd)
	{
		$reservation = Doctrine_Query::create()
			->select('r.id, r.hallId, h.name AS hall, r.fromTime, r.toTime, u.name AS creator')
			->from('Reservation r')
			->innerJoin('r.Hall h')
			->innerJoin('r.User u')
			->where('r.id=?', $rd->getParameter('id'))
			->fetchOne(array(), Doctrine::HYDRATE_ARRAY);

		if ($reservation === null) return $this->forward404();

		settype($reservation['fromTime'], 'int');
		settype($reservation['toTime'], 'int');

		$this->setAttribute('reservation', $reservation);

   	return 'Input';
	}

	public function executeWrite(AgaviRequestDataHolder $rd)
	{
		$reservation = $this->getTable('Reservation')->find($rd->getParameter('id'));

		if (!$reservation instanceof Reservation) return 'Error';

		$changeStatus = $rd->getParameter('type', 'reject');

  	$reservation->$changeStatus();

		$this->setAttribute('type', $changeStatus);


  	return 'Success';
	}
}